﻿using UnityEngine;
using System;
using System.Collections;

public class CameraFade : MonoSingleton<CameraFade>
{
    enum FadeState
    {
        None,
        In,
        Out,
        // OutIn,
    };

    FadeState fadeState = FadeState.In;
    float fadeTimer = 0.0f;
    float fadeTimerMax = 0.0f;
    bool  isEnableInput = true;

    public const float fadeInDelay = 1.8f;
    public const float fadeOutDelay = 0.5f;

    public Texture whiteTexture;
    public event Action delegateFunc;

    public bool drawFps = false;

    void Start()
    {
        if (!whiteTexture)
            whiteTexture = Resources.Load<Texture>("images/White"); //CreateWhiteTexture();
    }

    void Update()
    {
        
    }

    public bool IsFade()
    {
        return fadeState != FadeState.None;
    }

	public void FadeIn(float time, Action func/* = null*/)
    {
        EnableInput(false);
        fadeState = FadeState.In;
        fadeTimer = time;
        fadeTimerMax = time;
        delegateFunc = func;
    }

    public void FadeOut(float time, Action func/* = null*/)
    {
        EnableInput(false);
        fadeState = FadeState.Out;
        fadeTimer = time;
        fadeTimerMax = time;
        delegateFunc = func;
    }


    void OnGUI()
    {
        if (fadeState != FadeState.None)
        {
            float alpha;
            if (fadeState == FadeState.In)
            {
                alpha = fadeTimer / fadeTimerMax;
            }
            else //if (fadeState == FadeState.Out)
            {
                alpha = 1.0f - fadeTimer / fadeTimerMax;
            }

            GUI.color = new Color(255f, 255f, 255f, alpha);
            GUI.depth = -1000;

            if (whiteTexture)
            {
                GUI.DrawTexture(new Rect(-50, -50, Screen.width + 50, Screen.height + 50), whiteTexture);
            }
        }

        if (fadeTimer > 0.0f)
        {
            fadeTimer -= Time.deltaTime;
            if (fadeTimer <= 0.0f)
            {
                fadeTimer = 0.0f;
                if (fadeState == FadeState.In)
                {
                    //GUI.enabled = true;
                    EnableInput(true);
                    fadeState = FadeState.None;

                    if(delegateFunc != null)
                        delegateFunc();
                }
                //else if (fadeState == FadeState.OutIn)
                //{
                //    DoFadeIn();
                //}
                else if (fadeState == FadeState.Out)
                {
                    if(delegateFunc != null)
                        delegateFunc();
                }
            }
        }
    }

    public bool IsEnableInput()
    {
        return isEnableInput;
    }

    public void EnableInput(bool isEnable)
    {
        isEnableInput = isEnable;
        if (Application.HasProLicense())
        {
            for (int i = 0; i < UICamera.list.size; ++i)
            {
                UICamera cam = UICamera.list.buffer[i];
                cam.useMouse = isEnable;
                cam.useTouch = isEnable;
                cam.useKeyboard = isEnable;
                cam.useController = isEnable;
            }
        }
        else
        {
            UICamera[] cams = GameObject.FindObjectsOfType(typeof(UICamera)) as UICamera[];
            foreach (UICamera cam in cams)
            {
                cam.useMouse = isEnable;
                cam.useTouch = isEnable;
                cam.useKeyboard = isEnable;
                cam.useController = isEnable;
            }
        }
    }
}